﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NENAData.Factory;
using NENAData.EF;
using NENAData.Transactions;
using System.Data.SqlClient;

namespace NENAData.Events
{
    public class Slots
    {
        private static NENA_MembershipEntities GetConnection()
        {
            var x = new SQLMembership();
            return x.DBConnection();
        }

        private static string GetSQLString()
        {
            var x = new SQLMembership();
            return x.GetMembershipDBString();
        }

        public List<EF.Slot> GetSlotsByEvent(int intEventId)
        {

            string strSql = @"select Slot.SlotId ,Slot.SlotGroup ,Slot.Description ,Slot.Description_Long ,Slot.Type,
                    	        Slot.Time_Start ,Slot.Time_End ,Slot.Active
                                        from Slot
                                    join SlotGroup on Slot.SlotId = SlotGroup.SlotId
                                    join Events on SlotGroup.EventId = Events.EventId
                                where Events.EventId = @EventId";

            SqlConnection objSqlCon = new SqlConnection(GetSQLString());
            SqlCommand cmd = new SqlCommand(strSql, objSqlCon);
            cmd.Parameters.AddWithValue("@EventId", intEventId);

            objSqlCon.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            //create the return entity
            List<EF.Slot> lSlot = new List<EF.Slot>();

            if (!dr.HasRows)
            {
                return null;
            }

            //NENAData.Extensions.IsNull(dr["Speaker_URL"], "")
            while (dr.Read())
            {

                lSlot.Add(new EF.Slot
                {
                    SlotId = (int)dr["SlotId"],
                    SlotGroup = (int)dr["SlotGroup"],
                    Type = (string)dr["SlotType"],
                    Time_Start = (DateTime)dr["Time_Start"],
                    Time_End = (DateTime)dr["Time_End"],
                    Description = (string)dr["Description"],
                    Description_Long = NENAData.Extensions.IsNull(dr["Description_Long"], ""),
                    Active = (bool)dr["Active"]
                });

            }
            dr.Close();

            return lSlot;
        }

    }
}
